﻿namespace ElisVK.Data.AudioSources
{
    using System.Collections.ObjectModel;
    using System.Text;
    using Utils;

    class VKGroupAudioSource : AudioSource
    {
        private readonly GroupInfo _groupInfo;

        public VKGroupAudioSource(GroupInfo groupInfo)
            : base(groupInfo.Name)
        {
            _groupInfo = groupInfo;
        }

        public override ObservableCollection<Audio> GetAudios()
        {
            var audios = new ObservableCollection<Audio>();

            if (_groupInfo.GroupID == 0)
            {
                return audios;
            }

            var buff = VKHelper.DownloadData(
                string.Format("https://api.vkontakte.ru/method/audio.get.xml?gid={0}&access_token={1}", _groupInfo.GroupID, App.SessionInfo.AccessToken));

            audios = new ObservableCollection<Audio>(VKHelper.ParseAudiosFromXML(Encoding.UTF8.GetString(buff)));

            foreach (var audio in audios)
            {
                audio.OwnerID = _groupInfo.GroupID;
            }

            return audios;
        }
    }
}
